package com.shun.upms.action;


import cn.dev33.satoken.stp.StpUtil;
import com.shun.common.R;
import com.shun.common.RC;
import com.shun.common.log.LogType;
import com.shun.common.log.annotation.ShunLog;
import com.shun.upms.common.UpmsConstant;
import com.shun.upms.dto.SysAdminMenuDto;
import com.shun.upms.service.ISysAdminMenuService;
import com.shun.upms.vo.SysAdminMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author clever
 * @since 2021-09-27
 */
@Slf4j
@RestController
@RequestMapping("/upms/menu")
@Api(tags = "管理平台菜单API")
public class SysAdminMenuAction {

    @Autowired
    private ISysAdminMenuService adminMenuService;

    /**
     * 新增角色
     * @return
     */
    @PostMapping("/v1/addAdminMenu")
    @ApiOperation(value = "新增菜单")
    @ShunLog(title = "菜单",logType = LogType.INSERT)
    public R addAdminMenu(@RequestBody @Validated SysAdminMenuDto dto){
        log.debug("SysAdminRoleDto:{}",dto);
        return adminMenuService.addAminMenu(dto);
    }

    /**
     * 删除菜单
     * @return
     */
    @GetMapping("/v1/delAdminMenu/{menuId}")
    @ApiOperation(value = "菜单删除")
    @ShunLog(title = "删除",logType = LogType.DELETE)
    public R delAdminMenu(@PathVariable("menuId") String menuId){
        log.debug("menuId:{}",menuId);
        return adminMenuService.delMenuByMenuId(menuId);
    }

    /**
     * 删除角色
     * @return
     */
    @PostMapping("/v1/editAdminMenu")
    @ApiOperation(value = "菜单编辑")
    @ShunLog(title = "编辑",logType = LogType.UPDATE)
    public R editAdminMenu(@RequestBody SysAdminMenuDto menuDto){
        log.debug("menuDto:{}",menuDto);
        return adminMenuService.editMenuByMenuId(menuDto);
    }

    /**
     * 菜单详细信息，根据MenuId
     * @return
     */
    @PostMapping("/v1/getAdminMenu/{menuId}")
    @ApiOperation(value = "查询菜单详细")
    public R getAdminMenu(@PathVariable("menuId") String roleId){
        log.debug("SysAdminRoleId:{}",roleId);
        SysAdminMenu role = adminMenuService.getById(roleId);
        return (role != null)?R.suc(role): RC.FEIGN_FAIL.toR("没有菜单信息！");
    }

    /**
     * 获取菜单分页信息
     * @return
     */
    @PostMapping("/v1/pageAdminMenu")
    @ApiOperation(value = "角色分页")
    public R pageAdminMenu(@RequestBody SysAdminMenuDto dto){
        return adminMenuService.pageAdminMenu(dto);
    }

    /**
     * 获取按钮信息列表
     * @return
     */
    @PostMapping("/v1/getUserBtns")
    @ApiOperation(value = "获得用户按钮")
    public R getUserBtns( ){
        String userId = StpUtil.getLoginIdAsString();
        return adminMenuService.getBtnsByUser(userId);
    }

    /**
     * 前端获取用户的菜单信息列表
     * @return
     */
    @PostMapping("/v1/getUserMenus")
    @ApiOperation(value = "获取用户的菜单信息【前端】")
    public R getUserMenus( ){
        return adminMenuService.getMenusByUser();
    }

    @PostMapping("/v1/getNameAndId")
    @ApiOperation(value = "获取菜单名称和ID的信息")
    public R getNameAndId(){
        log.debug("获取菜单名称和ID的列表信息！");
        return adminMenuService.getMenuNameAndId();
    }

    @PostMapping("/v1/getMenuTreeNodes")
    @ApiOperation(value = "获取左侧树形菜单节点列表")
    public R getMenuTreeNodes(){
        log.debug("获取左侧树形菜单节点列表！");
        return R.suc(adminMenuService.getTreeMenuNodes());
    }

    @PostMapping("/v1/getMenuTreeNodesAndBtn")
    @ApiOperation(value = "获取左侧树形菜单节点列表")
    public R getMenuTreeNodesAndBtn(){
        log.debug("获取左侧树形菜单节点列表！");
        return R.suc(adminMenuService.getTreeMenuNodes(null, UpmsConstant.MENU_ROOT_ID));
    }

    @PostMapping("/v1/getMenuNodes")
    @ApiOperation(value = "获取菜单节点列表")
    public R getMenuNodes(){
        log.debug("获取菜单节点列表！");
        return adminMenuService.getMenuNodes();
    }

    @PostMapping("/v1/getMenuNodesById/{menuId}")
    @ApiOperation(value = "获取菜单节点列表")
    public R getMenuNodesById(@PathVariable("menuId") String menuId){
        log.debug("获取菜单节点列表！menuId:{}",menuId);
        return adminMenuService.getMenuNodesByMenuId(menuId);
    }

    /**
     * 根据用户ID获取,当前用户的菜单ID列表
     * @return
     */
    @PostMapping("/v1/getBtnIdsByRoleId/{roleId}")
    @ApiOperation(value = "获取角色的菜单节点列表")
    public R getMenuIdsByRoleId(@PathVariable("roleId") String roleId){
        log.debug("roleId:{}",roleId);
        return adminMenuService.getBtnIdsByRole(roleId);
    }
}

